<?php

/*

  Action - Attack	$4
  Each other player reveals the top 2 cards of his deck.
  If they revealed any Treasure cards, they trash one of them that you choose.
  You may gain any or all of these trashed cards.
  They discard the other revealed cards.

 */

class Thief extends Card implements iAction, iKingdom, iAttack, iBasic {

    public function __construct() {
        parent::__construct("Thief", 4);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
             "Each other player reveals the top 2 cards of his deck.  If they revealed any Treasure cards, they trash one of them that you choose.  You may gain any or all of these trashed cards.  They discard the other revealed cards.");
    }
    
    private function helper(Game $game, $name) {

        $cards = $game->removeCardsFromDrawDeck(2, $name);

         ScreenDisplay::addScreenMessage($game, true, " reveal " . nicePrint($cards), " reveals " . ScreenDisplay::$LMSG, $name);

        $has_treasure_cards = false;
        $treasure_cards = array();
        foreach ($cards as $card) {
            if ($card instanceof iTreasure) {
                $has_treasure_cards = true;
                $treasure_cards[] = $card;
            } else {
                $game->putCardInDiscardDeck($card, $name);
            }
        }

		$treasure_cards = getUniqueCards($treasure_cards);
        $trashed_card = null;
        //remember that the other player treasure cards are currently in the 'air' we should decide where to put them
        if (sizeof($treasure_cards) == 2) {

            $output = MySocket::GetUserReply($game, Input_ChooseOne, $treasure_cards, 1, 1, null, "Choose a card to trash for " . $name);
            $trashed_card = $output[0];
            $trashed_card = n2c($trashed_card);

            $card1_name = $treasure_cards[0];
            $card2_name = $treasure_cards[1];
            $other_card = null;
            if ($card1_name->getKey() == $trashed_card->getKey()) {
                $other_card = $card2_name;
            } else if ($card2_name->getKey() == $trashed_card->getKey()) {
                $other_card = $card1_name;
            }
            //we discard the not chosen card
            $game->putCardInDiscardDeck($other_card, $name);
        } else if (sizeof($treasure_cards) == 1) {
            $trashed_card = $treasure_cards[0];
        }

        if ($trashed_card) {
            //trashing the coosen card
            $game->putCardInTrash($trashed_card);
             ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($trashed_card), " trashes " . ScreenDisplay::$LMSG, $name);
            
            $options = array(new Option(1, "Yes"), new Option(2, "No"));
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Gain " . $trashed_card->getValue() . " from trash?");
            $answer = $output[0];
            if ($answer == "Yes") {
                $trashed_card = $game->moveCardFromPileToPile($trashed_card, Trash, DiscardPile);
                 ScreenDisplay::addScreenMessage($game, true, " gain " . nicePrint($trashed_card). " from the trash", " gains " . ScreenDisplay::$LMSG. " from the trash");
            }
        }

        $game->cardActionFinished();
        return true;
    }

    public function play(Game $game, $isRealCard) {
        $game->attack($this);
        $names = $game->getOtherPlayersNames(true);

        foreach ($names as $name) {
            $this->helper($game, $name);
        }
        $game->cardActionFinished();
        return true;
    }

}

?>